package cn.zhaoyuening.zshop4j.search.service.impl;

import cn.zhaoyuening.zshop4j.common.bean.SearchItemBean;
import cn.zhaoyuening.zshop4j.common.bean.SearchResult;
import cn.zhaoyuening.zshop4j.common.bean.ZShop4jResponceResult;
import cn.zhaoyuening.zshop4j.manager.mapper.SearchItemMapper;
import cn.zhaoyuening.zshop4j.search.dao.SearchDao;
import cn.zhaoyuening.zshop4j.search.service.SearchService;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * Created by 赵岳宁 on 2017/5/1.
 */
@Service("searchService")
public class SearchServiceImpl implements SearchService {
    @Autowired
    private SearchDao searchDao;
    @Autowired
    private SearchItemMapper searchItemMapper;
    @Autowired
    private SolrServer solrServer;

    @Override
    public ZShop4jResponceResult importAllItems() throws IOException, SolrServerException {
        // 1、查询所有商品数据。
        List<SearchItemBean> itemList = searchItemMapper.getItemList();
        // 2、创建一个SolrServer对象。
        // 3、为每个商品创建一个SolrInputDocument对象。
        for (SearchItemBean searchItem : itemList) {
            SolrInputDocument document = new SolrInputDocument();
            // 4、为文档添加域
            document.addField("id", searchItem.getId());
            document.addField("item_title", searchItem.getTitle());
            document.addField("item_sell_point", searchItem.getSell_point());
            document.addField("item_price", searchItem.getPrice());
            document.addField("item_image", searchItem.getImage());
            document.addField("item_category_name", searchItem.getCategory_name());
            document.addField("item_desc", searchItem.getItem_desc());
            // 5、向索引库中添加文档。
            solrServer.add(document);
        }
        //提交修改
        solrServer.commit();
        // 6、返回TaotaoResult。
        return ZShop4jResponceResult.ok();
    }

    @Override
    public SearchResult search(String queryString, int page, int rows) throws Exception {
        // 1、创建一个SolrQuery对象。
        SolrQuery query = new SolrQuery();
        // 2、设置查询条件
        query.setQuery(queryString);
        // 3、设置分页条件
        query.setStart((page - 1) * rows);
        query.setRows(rows);
        // 4、需要指定默认搜索域。
        query.set("df", "item_title");
        // 5、设置高亮
        query.setHighlight(true);
        query.addHighlightField("item_title");
        query.setHighlightSimplePre("<em style=\"color:red\">");
        query.setHighlightSimplePost("</em>");
        // 6、执行查询，调用SearchDao。得到SearchResult
        SearchResult result = searchDao.search(query);
        // 7、需要计算总页数。
        long recordCount = result.getRecordCount();
        long pageCount = recordCount / rows;
        if (recordCount % rows > 0) {
            pageCount++;
        }
        result.setPageCount(pageCount);
        // 8、返回SearchResult
        return result;
    }
}
